package com.mdinic.blogovi.model;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

@Component
@Configurable
@RooDataOnDemand(entity = BlogPost.class)
public class BlogPostDataOnDemand {

    private final Random rnd = new SecureRandom();

    private List<BlogPost> data;

    @Autowired
    private BlogDataOnDemand blogDataOnDemand;

    @Autowired
    private PersonDataOnDemand personDataOnDemand;

    @Autowired
    private BlogTypeDataOnDemand blogTypeDataOnDemand;

    public void setReported(BlogPost obj, int index) {
        Integer reported = new Integer(index);
        obj.setReported(reported);
    }

    public BlogPost getNewTransientBlogPost(int index) {
        BlogPost obj = new BlogPost();
        setApproved(obj, index);
        setBlog(obj, index);
        setClick(obj, index);
        setComment(obj, index);
        setDescription(obj, index);
        setHeadline(obj, index);
        setPerson(obj, index);
        setPosted(obj, index);
        setPromoted(obj, index);
        setThumb(obj, index);
        setType(obj, index);
        setUrl(obj, index);
        setView(obj, index);
        setVoteDown(obj, index);
        setVoteUp(obj, index);
        return obj;
    }

    public void setApproved(BlogPost obj, int index) {
        Boolean approved = Boolean.TRUE;
        obj.setApproved(approved);
    }

    public void setBlog(BlogPost obj, int index) {
        Blog blog = blogDataOnDemand.getRandomBlog();
        obj.setBlog(blog);
    }

    public void setClick(BlogPost obj, int index) {
        Integer click = new Integer(index);
        obj.setClick(click);
    }

    public void setComment(BlogPost obj, int index) {
        Integer comment = new Integer(index);
        obj.setComment(comment);
    }

    public void setDescription(BlogPost obj, int index) {
        String description = "descriptionxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_"
                + index;
        if (description.length() > 1000) {
            description = description.substring(0, 1000);
        }
        obj.setDescription(description);
    }

    public void setHeadline(BlogPost obj, int index) {
        String headline = "headline_" + index;
        if (headline.length() > 256) {
            headline = new Random().nextInt(10) + headline.substring(1, 256);
        }
        obj.setHeadline(headline);
    }

    public void setPerson(BlogPost obj, int index) {
        Person person = personDataOnDemand.getRandomPerson();
        obj.setPerson(person);
    }

    public void setPosted(BlogPost obj, int index) {
        Date posted = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
                Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(
                Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(
                Calendar.SECOND)
                + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setPosted(posted);
    }

    public void setPromoted(BlogPost obj, int index) {
        Date promoted = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
                Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(
                Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(
                Calendar.SECOND)
                + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setPromoted(promoted);
    }

    public void setThumb(BlogPost obj, int index) {
        String thumb = "thumb_" + index;
        obj.setThumb(thumb);
    }

    public void setType(BlogPost obj, int index) {
        BlogType type = blogTypeDataOnDemand.getRandomBlogType();
        obj.setType(type);
    }

    public void setUrl(BlogPost obj, int index) {
        String url = "url_" + index;
        if (url.length() > 256) {
            url = new Random().nextInt(10) + url.substring(1, 256);
        }
        obj.setUrl(url);
    }

    public void setView(BlogPost obj, int index) {
        Integer view = new Integer(index);
        obj.setView(view);
    }

    public void setVoteDown(BlogPost obj, int index) {
        Integer voteDown = new Integer(index);
        obj.setVoteDown(voteDown);
    }

    public void setVoteUp(BlogPost obj, int index) {
        Integer voteUp = new Integer(index);
        obj.setVoteUp(voteUp);
    }

    public BlogPost getSpecificBlogPost(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        BlogPost obj = data.get(index);
        Long id = obj.getId();
        return BlogPost.findBlogPost(id);
    }

    public BlogPost getRandomBlogPost() {
        init();
        BlogPost obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return BlogPost.findBlogPost(id);
    }

    public boolean modifyBlogPost(BlogPost obj) {
        return false;
    }

    public void init() {
        int from = 0;
        int to = 10;
        data = BlogPost.findBlogPostEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'BlogPost' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }

        data = new ArrayList<BlogPost>();
        for (int i = 0; i < 10; i++) {
            BlogPost obj = getNewTransientBlogPost(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage())
                            .append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }

}
